from rest_framework.views import APIView
from apps.actions.models import ActionTab, RedEnvelop, RedEnvelopReceive
from apps.actions.serializers import ActionSerializer
from rest_framework.pagination import LimitOffsetPagination
from apps.utils.authentication import TokenAuthentication
from apps.utils.responses import response_ok, response_error
from apps.actions.serializers import (
    RedEnvelopCreateSerializer, RedEnvelopSerializer,
    RedEnvelopReceiveSerializer, RedEnvelopInfoSerializer,
    RedEnvelopChanllengeSerializer,
)


class ActionRecommendAPIView(APIView):

    authentication_classes = (TokenAuthentication, )

    def get(self, request, *args, **kwargs):
        actions = ActionTab.objects.filter(is_deleted=False).all()
        paginator = LimitOffsetPagination()
        page_qs = paginator.paginate_queryset(actions, request)
        serializer = ActionSerializer(page_qs, many=True)
        return paginator.get_paginated_response(serializer.data)


class RedEnvelopCreateAPIView(APIView):

    authentication_classes = (TokenAuthentication,)

    def post(self, request, *args, **kwargs):
        ser = RedEnvelopCreateSerializer(data=request.data, context={'request': request})
        ser.is_valid(raise_exception=True)
        ser.save()
        return response_ok({}, message='红包创建成功')


class RedEnvelopInfoAPIView(APIView):

    authentication_classes = (TokenAuthentication,)

    def get(self, request, uuid, *args, **kwargs):
        if not uuid:
            return response_error({}, message='uuid不存在')

        qs = RedEnvelop.objects.filter(id=uuid)
        if not qs.exists():
            return response_error({}, message='红包不存在')

        ser = RedEnvelopInfoSerializer(qs.first())
        return response_ok(ser.data, message='获取我的红包信息成功')


class RedEnvelopSendRecordsAPIView(APIView):

    authentication_classes = (TokenAuthentication, )

    def get(self, request, *args, **kwargs):
        user = getattr(request, 'current_user', None)
        red_envelops = RedEnvelop.objects.filter(send_user_id=user.id).order_by('-created_time')
        ser = RedEnvelopSerializer(red_envelops.all(), many=True)
        total_send_amount = sum([s.amount for s in red_envelops.all()])
        total_receive_amount = sum([s.total_receive_amount_of_cent for s in red_envelops.all()])
        total_send_count = red_envelops.count()
        return response_ok({
            'list': ser.data,
            'total_send_amount': float(total_send_amount) * 0.01,
            'total_receive_amount': float(total_receive_amount) * 0.01,
            'total_send_count': total_send_count,
        }, message='获取我的红包信息成功')


class RedEnvelopReceiveAPIView(APIView):

    authentication_classes = (TokenAuthentication, )

    def get(self, request, *args, **kwargs):
        user = getattr(request, 'current_user', None)
        red_envelops_res = RedEnvelopReceive.objects.filter(receive_user_uuid=user.id).order_by('-created_time')
        ser = RedEnvelopReceiveSerializer(red_envelops_res, many=True)
        total_receive_amount = sum([o.receive_amount_of_cent for o in red_envelops_res.all()])
        total_receive_count = red_envelops_res.count()
        return response_ok({
            'total_receive_amount': float(total_receive_amount) * 0.01,
            'total_receive_count': total_receive_count,
            'list': ser.data,
        })


class RedEnvelopChallengeAPIView(APIView):

    authentication_classes = (TokenAuthentication, )

    def post(self, request, *args, **kwargs):
        ser = RedEnvelopChanllengeSerializer(data=request.data, context={'request': request})
        ser.is_valid(raise_exception=True)
        res = ser.save()
        return (
            response_ok({}, message='挑战成功')
            if res else
            response_error({}, '挑战失败')
        )
